﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BaiDuSitemap.Setting
{
    public partial class Group : Form
    {
        #region Attributes

        public bool isAdd = true;
        public CustomListView clv = null;
        private string gname = "";

        #endregion

        public Group()
        {
            InitializeComponent();
        }

        private void Group_Load(object sender, EventArgs e)
        {
            if (!isAdd)
            {
                gname = this.tbGroup.Text = this.clv.SelectedItems[0].SubItems[1].Text;
                this.tbComment.Text = this.clv.SelectedItems[0].SubItems[2].Text;
            }
        }

        #region Event

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.tbGroup.Text.Length == 0)
            {
                MessageBox.Show("分组名称不能为空");
                return;
            }

            if (isAdd)
            {
                int result = DBO.Group.Add(this.tbGroup.Text, this.tbComment.Text);
                switch (result)
                {
                    case -2:
                        MessageBox.Show("分组名称 " + this.tbGroup.Text + " 已存在");
                        return;
                    case 1:
                        MessageBox.Show("操作成功");
                        ListViewItem item=new ListViewItem();
                        item.SubItems[0].Text=(clv.Items.Count+1).ToString();
                        item.SubItems.Add(this.tbGroup.Text);
                        item.SubItems.Add(this.tbComment.Text);
                        clv.Items.Add(item);
                        break;
                    default:
                        MessageBox.Show("操作失败，错误代码：" + result.ToString());
                        return;
                }
            }
            else
            {
                int result = DBO.Group.Modify(gname, this.tbGroup.Text, this.tbComment.Text);
                switch (result)
                {
                    case -2:
                        MessageBox.Show("分组名称 " + this.tbGroup.Text + " 已存在");
                        return;
                    case 1:
                        MessageBox.Show("操作成功");
                        clv.SelectedItems[0].SubItems[1].Text = this.tbGroup.Text;
                        clv.SelectedItems[0].SubItems[2].Text = this.tbComment.Text;
                        break;
                    default:
                        MessageBox.Show("操作失败，错误代码：" + result.ToString());
                        return;
                }
            }

            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion
    }
}
